<?php

namespace app\admin\validate;

use think\Validate;

class Returnorder extends Validate
{
    protected $rule = [
        'id' => 'require|number',
        'pageIndex' => 'require|number',
        'pageSize' => 'require|number',
        'ids' => 'require|array',
        'customer_id' => 'require|number',
        'returnorder_type' => 'require|in:1,2,3',
        'order_id' => 'require|number',
        'signatory' => 'require|number',
        'partner' => 'require|array',
        'paid_amount' => 'require',
        'status' => 'require|number',
        'return_reason' => 'require|number',
        'return_status' => 'require|number',
        'voucher_id' => 'require|string',
        'amount' => 'require',
        'payment_list' => 'require|array',
        'stock_list' => 'require|array'
    ];

    protected $scene = [
        'getList' => ['pageIndex', 'pageSize'],
        'save' => ['returnorder_type', 'customer_id', 'order_id', 'signatory', 'signing_date', 'owner', 'return_reason', 'payment_list', 'returnorder_method'],
        'detail' =>  ['id'],
        'getOrderList' => ['pageIndex', 'pageSize'],
        'update' => ['id', 'returnorder_type', 'customer_id', 'order_id', 'signatory', 'signing_date', 'owner', 'return_reason', 'returnorder_method'],
        'returnorderPayment' => ['id', 'voucher_id', 'amount'],
        'cancel' => ['id'],
        'stock' => ['id', 'stock_list'],
        'getOperationRecords' => ['pageIndex', 'pageSize']
    ];

    /**
     * 自定义验证方法：
     * @param array $data 所有数据
     * @return bool|string
     */
    /**
     * 检查returnorder_method是否必填
     */
    public function customValidationRules($data)
    {

        if ($data['returnorder_type'] == 3) {
            if (empty($data['products_list'])) {
                return ['code' => 1, 'meaasge' => '当退货退款单类型为"退款且退货"时，退货商品不能为空'];
            }
            if (empty($data['returnorder_method'])) {
                return ['code' => 1, 'meaasge' => '当退货退款单类型为"退款且退货"时，退货方式不能为空'];
            }
            if (!in_array($data['returnorder_method'], [1, 2])) {
                return ['code' => 1, 'meaasge' => '当退货退款单类型为"退款且退货"时，退货方式只能是"上门提货"或"客户寄送"'];
            }
            if (1 == $data['returnorder_method'] && empty($data['delivery_address'])) {
                return ['code' => 1, 'meaasge' => '当退货方式为"上门提货"时，提货地址不能为空'];
            }
            if (2 == $data['returnorder_method'] && empty($data['tracking_number'])) {
                return ['code' => 1, 'meaasge' => '当退货方式为"客户寄送"时，快递单号不能为空'];
            }
            if (2 == $data['returnorder_method'] && empty($data['express_company'])) {
                return ['code' => 1, 'meaasge' => '当退货方式为"客户寄送"时，快递单号不能为空'];
            }
        }
        return true;
    }
}
